package com.softserveinc.settings;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

import com.softserveinc.data.help.MovieListType;

public class AppWidgetSettingManager {
	protected static final String TAG = AppWidgetSettingManager.class
			.getSimpleName();

	private static final String MOVIE_LIST_SIZE = "widget_movie_list_size";
	private static final String MOVIE_LIST_ORDER = "widget_movie_list_order";
	private static final String UPDATE_WIDGET_UI = "widget_update_widget_ui";
	private static final String UPDATE_WIDGET_UI_DATA = "widget_update_widget_data";

	private static final String CURRENT_MOVIE_INDEX = "widget_current_movie_index";

	private static final String DEFAULT_UPDATE_PERIOD = "5";
	private static final String DEFAULT_UPDATE_DATA_PERIOD = "30";
	private static final String DEFAULT_RATING = "rating";
	private static final String DEFAULT_LIST_SIZE = "10";
	private static final int DEFAULT_MOVIE_INDEX = 0;

	private static final int MILISECONDS_IN_SECOND = 1000;
	private static final int SECONDS_IN_MINUTE = 60;

	private final Context context;

	public AppWidgetSettingManager(final Context context) {
		this.context = context;
	}

	public void saveAppWidgetPreferences(final int appWidgetID) {
		SharedPreferences preference = PreferenceManager
				.getDefaultSharedPreferences(context);

		String listSize = preference.getString(MOVIE_LIST_SIZE,
				DEFAULT_LIST_SIZE);
		String listOrder = preference.getString(MOVIE_LIST_ORDER,
				DEFAULT_RATING);
		String updateUi = preference.getString(UPDATE_WIDGET_UI,
				DEFAULT_UPDATE_PERIOD);
		String updateUiData = preference.getString(UPDATE_WIDGET_UI_DATA,
				DEFAULT_UPDATE_DATA_PERIOD);

		Editor editor = preference.edit();

		editor.putString(MOVIE_LIST_ORDER + appWidgetID, listOrder);
		editor.putString(MOVIE_LIST_SIZE + appWidgetID, listSize);
		editor.putString(UPDATE_WIDGET_UI + appWidgetID, updateUi);
		editor.putString(UPDATE_WIDGET_UI_DATA + appWidgetID, updateUiData);

		editor.putInt(CURRENT_MOVIE_INDEX + appWidgetID, DEFAULT_MOVIE_INDEX);

		editor.commit();
	}

	public void removeAppWidgetPreferences(final int appWidgetID) {
		SharedPreferences preferences = PreferenceManager
				.getDefaultSharedPreferences(context);

		Editor editor = preferences.edit();

		editor.remove(MOVIE_LIST_ORDER + appWidgetID);
		editor.remove(MOVIE_LIST_SIZE + appWidgetID);
		editor.remove(UPDATE_WIDGET_UI + appWidgetID);
		editor.remove(UPDATE_WIDGET_UI_DATA + appWidgetID);
		editor.remove(CURRENT_MOVIE_INDEX + appWidgetID);

		editor.commit();
	}

	public int getUpdateUIPeriodInMilieconds(final int appWidgetID) {
		SharedPreferences preference = PreferenceManager
				.getDefaultSharedPreferences(context);

		String updateUi = preference.getString(UPDATE_WIDGET_UI,
				DEFAULT_UPDATE_PERIOD);

		return Integer.valueOf(updateUi) * MILISECONDS_IN_SECOND;
	}

	public int getUpdateUIDataPeriodInMilieconds(final int appWidgetID) {
		SharedPreferences preference = PreferenceManager
				.getDefaultSharedPreferences(context);

		String updateUi = preference.getString(UPDATE_WIDGET_UI_DATA,
				DEFAULT_UPDATE_DATA_PERIOD);

		return Integer.valueOf(updateUi) * MILISECONDS_IN_SECOND
				* SECONDS_IN_MINUTE;
	}

	public int getMovieSize(final int appWidgetID) {
		SharedPreferences preference = PreferenceManager
				.getDefaultSharedPreferences(context);

		String listSize = preference.getString(MOVIE_LIST_SIZE,
				DEFAULT_LIST_SIZE);

		return Integer.valueOf(listSize);
	}

	public MovieListType getMovieListType(final int appWidgetID) {
		SharedPreferences preference = PreferenceManager
				.getDefaultSharedPreferences(context);

		String listOrder = preference.getString(MOVIE_LIST_ORDER,
				DEFAULT_RATING);

		return MovieListType.getListTypeByOrderName(listOrder);
	}

	public int getCurrentMovieIndex(final int appWidgetID) {
		SharedPreferences preference = PreferenceManager
				.getDefaultSharedPreferences(context);

		int index = preference.getInt(CURRENT_MOVIE_INDEX + appWidgetID,
				DEFAULT_MOVIE_INDEX);

		return index;
	}

	public void updateCurrentMovieIndex(final int appWidgetID) {
		int index = getCurrentMovieIndex(appWidgetID);
		int listSize = getMovieSize(appWidgetID);

		index++;

		if (index == listSize) {
			index = DEFAULT_MOVIE_INDEX;

		}

		SharedPreferences preference = PreferenceManager
				.getDefaultSharedPreferences(context);

		Editor editor = preference.edit();

		editor.putInt(CURRENT_MOVIE_INDEX + appWidgetID, index);

		editor.commit();
	}
}
